package com.smiier.skin;

import java.util.Date;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import cn.o.app.event.listener.OBitmapLoadCallBack;
import cn.o.app.io.Extra;
import cn.o.app.ui.OImageView;
import cn.o.app.ui.OTab;
import cn.o.app.ui.OView;
import cn.o.app.util.OUtil;

import com.evan.common.pullrefresh.PullToRefreshListView;
import com.lidroid.xutils.BitmapUtils;
import com.pifuke.adapter.QuestionAdapter;
import com.smiier.skin.net.entity.RelationShip;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.net.entity.UserWrapper;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;

@SuppressWarnings("deprecation")
public class PatientOfDoctorDetailActivity extends BasicActivity {
	public static class PatientOfDoctorDetailExtra extends Extra {
		public UserWrapper data;
	}

	protected PatientOfDoctorDetailExtra mExtra;

	protected PatientOfDoctorDetialHead mQuestionListHead;
	protected PatientOfDoctorDetialHead mProjectListHead;

	protected PullToRefreshListView mQuestionList;
	protected PullToRefreshListView mProjectList;

	protected QuestionAdapter mQuestionAdapter;

	protected BitmapUtils mBitmapUtils;

	protected Drawable mDrawableMale;
	protected Drawable mDrawableFemale;

	protected Drawable mDrawableOffline;
	protected Drawable mDrawableOnline;
	protected Drawable mDrawableProject;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mExtra = new PatientOfDoctorDetailExtra();
		if (!mExtra.getFrom(getIntent())) {
			finish();
			return;
		}

		this.setContentView(R.layout.activity_patient_of_doctor_detail);

		mBitmapUtils = new BitmapUtils(getContext());
		Resources res = this.getContext().getResources();
		mDrawableMale = res.getDrawable(R.drawable.sex_male);
		mDrawableFemale = res.getDrawable(R.drawable.sex_female);
		mDrawableOffline = res.getDrawable(R.drawable.source_from_offline);
		mDrawableOnline = res.getDrawable(R.drawable.source_from_online);
		mDrawableProject = res.getDrawable(R.drawable.source_from_project);

		init();
		setNavTitle(mExtra.data.RelationShip.Name);

		mQuestionListHead = new PatientOfDoctorDetialHead(getContext());
		mProjectListHead = new PatientOfDoctorDetialHead(getContext());

		mQuestionList = this.findViewById(R.id.question_list,
				PullToRefreshListView.class);
		mQuestionList.getListView().addHeaderView(mQuestionListHead);
		mProjectList = this.findViewById(R.id.project_list,
				PullToRefreshListView.class);
		mProjectList.getListView().addHeaderView(mProjectListHead);

		mQuestionAdapter = new QuestionAdapter();
		mQuestionList.setAdapter(mQuestionAdapter);

	}

	class PatientOfDoctorDetialHead extends OView {
		protected OImageView mLogoView;
		protected TextView mNameText;
		protected TextView mProfileText;
		protected TextView mVisitButton;
		protected TextView mSourceFromText;
		protected TextView mPhoneText;
		protected TextView mVisitTimeText;
		protected TextView mDiagnoseText;
		protected TextView mConditionText;
		protected OTab mTabBar;

		protected UserWrapper mDataProvider;

		public PatientOfDoctorDetialHead(Context context) {
			super(context);
		}

		@Override
		protected void onCreate(Context context, AttributeSet attrs) {
			this.setContentView(R.layout.view_doctor_detail_head);

			mLogoView = this.findViewById(R.id.logo, OImageView.class);
			mNameText = this.findViewById(R.id.name, TextView.class);
			mProfileText = this.findViewById(R.id.profile, TextView.class);
			mVisitButton = this.findViewById(R.id.visit, TextView.class);
			mSourceFromText = this.findViewById(R.id.source_from,
					TextView.class);
			mPhoneText = this.findViewById(R.id.phone, TextView.class);
			mVisitTimeText = this.findViewById(R.id.visit_time, TextView.class);
			mDiagnoseText = this.findViewById(R.id.diagnose, TextView.class);
			mConditionText = this.findViewById(R.id.condition, TextView.class);
			mTabBar = this.findViewById(R.id.tab_bar, OTab.class);
		}

		public void setDataProvider(UserWrapper dataProvider) {
			mDataProvider = dataProvider;
			Date today = new Date();
			String path = mDataProvider.User.Pic;
			if (path.lastIndexOf(".") == -1) {
				path += GlobalSettings.IMG_THUMB;
			}
			path = GlobalSettings.SERVER_IMG_URL + path;
			mBitmapUtils.display(mLogoView, path,
					new OBitmapLoadCallBack<View>());
			mNameText.setText(mDataProvider.User.Name);
			StringBuilder sb = new StringBuilder();
			if (mDataProvider.User.Sex == User.SEX_MALE) {
				mNameText.setCompoundDrawablesWithIntrinsicBounds(null, null,
						mDrawableMale, null);
				sb.append("男,");
			} else if (mDataProvider.User.Sex == User.SEX_FEMALE) {
				mNameText.setCompoundDrawablesWithIntrinsicBounds(null, null,
						mDrawableFemale, null);
				sb.append("女,");
			} else {
				mNameText.setCompoundDrawablesWithIntrinsicBounds(null, null,
						null, null);
			}
			sb.append(today.getYear() - mDataProvider.User.Birth.getYear());
			sb.append("岁");
			if (!OUtil.isEmpty(mDataProvider.RelationShip.Diagnose)) {
				sb.append(",");
				sb.append(mDataProvider.RelationShip.Diagnose);
			}
			mProfileText.setText(sb);
			if (mDataProvider.RelationShip.RSSource == RelationShip.SOURCE_OFFLINE) {
				mSourceFromText.setVisibility(View.VISIBLE);
				mSourceFromText.setText("线下导入");
				mSourceFromText.setCompoundDrawablesWithIntrinsicBounds(null,
						null, mDrawableOffline, null);
			} else if (mDataProvider.RelationShip.RSSource == RelationShip.SOURCE_ONLINE) {
				mSourceFromText.setVisibility(View.VISIBLE);
				mSourceFromText.setText("在线咨询");
				mSourceFromText.setCompoundDrawablesWithIntrinsicBounds(null,
						null, mDrawableOnline, null);
			} else {
				mSourceFromText.setVisibility(View.INVISIBLE);
			}
			mPhoneText.setText(mDataProvider.RelationShip.Mobile);
			/*
			 * mDataProvider.RelationShip.Visiting_Time.setFormat("yyyy-MM-dd");
			 * mVisitTimeText.setText(mDataProvider.RelationShip.Visiting_Time
			 * .toString());
			 */

			mVisitTimeText.setText(mDataProvider.RelationShip.Visiting_Time
					.toString());
			mDiagnoseText.setText(mDataProvider.RelationShip.Diagnose);
			mConditionText.setText(mDataProvider.RelationShip.Condition);
		}
	}

}